/* 
    Primarily this tool is used in conjunction with http://search.twitter.com by creating a search RSS url. This will "watch" for search terms and deliver the related post to your IM. 
    Copyright (C) 2008  Roger Filomeno

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package com.godie.bot;
import com.sun.syndication.feed.synd.*;
import com.sun.syndication.io.SyndFeedInput;
import com.sun.syndication.io.XmlReader;

import java.net.URL;
import java.util.Date;
import java.util.List;
import java.util.Iterator;


public class rss2im {

	public static void main(String[] args) {
		
		// Start configuration
		String xmpp_host = "talk.google.com";
		String xmpp_resource = "gmail.com"; //For Gtalk, use gmail.com
		String bot_xmppusername = "username";	//For Gtalk, use only the username without the @ and domain part
		String bot_xmpppassword = "password";
		String sendmessagesto = "user@gmail.com"; //must use the username@domain.name format
		String feed = "http://search.twitter.com/search.atom?q=%40godie+OR+%23godie";
		// End configuration
		// You should not edit the codes below unless you know what you're doing
		
		imbot imworker = new imbot(
				xmpp_host,
				xmpp_resource,
				bot_xmppusername,
				bot_xmpppassword,
				sendmessagesto);	
		
		Date lastpublished = new Date();
		lastpublished.setTime(0);
		
		Date latestpublished = new Date();
		
		while(true) {
			try {
				// Enter RSS feed URL here
				
				URL feedUrl = new URL(feed);
				Integer entrycounter = 0;
				
				
				XmlReader feedxml = new XmlReader(feedUrl);
			    SyndFeedInput input = new SyndFeedInput();
			    SyndFeed sf = input.build(feedxml);
			    List entries = sf.getEntries();
			    Iterator it = entries.iterator();
				while (it.hasNext()) {
					SyndEntry entry = (SyndEntry)it.next();
					
					if (lastpublished.compareTo(entry.getPublishedDate()) >= 0) break;
					if (entrycounter >=5) break;
					if (entrycounter == 0) latestpublished = entry.getPublishedDate();
					
					
					String sendmessage = entry.getAuthor().replaceAll("^(\\w)+ ", "") + ": " + entry.getTitle();
					System.out.println(sendmessage);
				    imworker.addmessage(sendmessage);
					entrycounter++;
					System.out.println(latestpublished);
					System.out.println(lastpublished);
					System.out.println(entry.getPublishedDate());
				}
				entrycounter = 0;
				lastpublished = latestpublished;
				Thread.sleep(10000);
			}
			catch (Exception e)
			{
				//do nothing
			}
		}
	}
}
